package gui;

import gui.AbstractViewPane.ViewMode;
import gui.nodes.AbstractNode;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.StringTokenizer;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import com.toedter.calendar.JCalendar;

import business.CongeBL;

public class CongeUIConsulter extends AbstractViewPane implements ActionListener {
	String debut = "";
	String fin = "";
	ViewMode currentViewMode;
	int congeId;
	int anneeEtudeId;
	JPanel general;
    JButton save;
    JButton annuler;
    
    JCalendar calDebut; 
    JCalendar calFin; 
	private int dayDeb;
	private int monthDeb;
	private int yearDeb;
	private int dayFin;
	private int monthFin;
	private int yearFin;
	private AbstractNode creator;

	
	public void drawCommandBox(JPanel commandBox) {
        JPanel buttonPanel = new JPanel(new BorderLayout());
        buttonPanel.add(new JSeparator(), BorderLayout.PAGE_START);
        buttonPanel.add(commandBox, BorderLayout.LINE_END);
        
        general.add(buttonPanel, BorderLayout.SOUTH);
	}
	
	public String getDebut() {
		return calDebut.getDayChooser().getDay()+"/"+calDebut.getMonthChooser().getMonth()+"/"+calDebut.getYearChooser().getYear();
	}

	public String getFin() {
		return calFin.getDayChooser().getDay()+"/"+calFin.getMonthChooser().getMonth()+"/"+calFin.getYearChooser().getYear();
	}

	public CongeUIConsulter(AbstractNode creator, ViewMode v, int conId, int aeId) {

		this.creator = creator;
		currentViewMode = v;
		congeId = conId;
		anneeEtudeId = aeId;
		
		if (congeId != -1) {
			CongeBL congeBL = new CongeBL(congeId);
			debut = congeBL.getDateDebutConge();
			fin = congeBL.getDateFinConge();
		}
		
/*    	setSize(500, 500);
		setTitle("Gestion des congés");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
*/		
		general = new JPanel();
		general.setLayout(new BorderLayout());
		
		general.setBorder(BorderFactory.createCompoundBorder(
		BorderFactory.createTitledBorder(getTitle()),
		BorderFactory.createEmptyBorder(5,5,5,5)));

				
        //Create and populate the panel.
        JPanel p = new JPanel(new SpringLayout());
        
        JLabel l = new JLabel("Début Congé:", JLabel.TRAILING);
        p.add(l);
        if (currentViewMode != ViewMode.AJOUT) {
	    	String tmp = debut.replace('/',' ');
	    	StringTokenizer tokenizer = new StringTokenizer(tmp);
	   	    dayDeb = Integer.parseInt(tokenizer.nextToken());
	   	    monthDeb = Integer.parseInt(tokenizer.nextToken());
	   	    yearDeb = Integer.parseInt(tokenizer.nextToken());
	        Date date = new Date(yearDeb-1900, monthDeb-1, dayDeb);
	        calDebut = new JCalendar(date);
        }
        else
        	calDebut = new JCalendar();
        l.setLabelFor(calDebut);
        p.add(calDebut);
        
        l = new JLabel("Fin Congé:", JLabel.TRAILING);
        p.add(l);
        if (currentViewMode != ViewMode.AJOUT) {
	        String tmp = fin.replace('/',' ');
	        StringTokenizer tokenizer = new StringTokenizer(tmp);
	   	    dayFin = Integer.parseInt(tokenizer.nextToken());
	   	    monthFin = Integer.parseInt(tokenizer.nextToken());
	   	    yearFin = Integer.parseInt(tokenizer.nextToken());
	        Date date = new Date(yearFin-1900, monthFin-1, dayFin);
	        calFin = new JCalendar(date);
        }
        else 
        	calFin = new JCalendar();
        l.setLabelFor(calFin);
        p.add(calFin);

        //Lay out the panel.
        SpringUtilities.makeCompactGrid(p,
                                        2, 2, //rows, cols
                                        6, 6,        //initX, initY
                                        6, 6);       //xPad, yPad
        
        general.add(p, BorderLayout.NORTH);
        add(general);
        
    	// Mode modification - afficher les boutons
        JPanel commandBox = new JPanel();
        save = new JButton("Enregistrer");
        save.addActionListener(this);
        commandBox.add(save);
        drawCommandBox(commandBox);
        

	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == save) {
			if (currentViewMode == ViewMode.AJOUT) {
				CongeBL congeBL = new CongeBL ();
				congeBL.ajouterConge(congeId, getDebut(), getFin(), anneeEtudeId);
				calDebut.setEnabled(false);
				calFin.setEnabled(false);
			}
			if (currentViewMode == ViewMode.MODIFICATION) {
				CongeBL congeBL = new CongeBL (congeId);
				congeBL.modifierConge(congeId, getDebut(), getFin(), anneeEtudeId);
				calDebut.setEnabled(false);
				calFin.setEnabled(false);
			}
			creator.refresh();
		}
//		this.dispose();
	}

	@Override
	public String getTitle() {
		return "Gestion des congés";
	}

	@Override
	public boolean hasBeenModified() {
		return (currentViewMode != ViewMode.CONSULTATION);
	}
}
